package org.mch.comm.lang;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描述：；<BR/>
 * 
 * @author MCH；Date：2007-11-21；<BR/>
 */
public class MTime implements Serializable, Comparable<Object> {
	private static final long serialVersionUID = -5749512554128290192L;

	private String value = null;

	public MTime() {
		super();
	}

	public MTime(long m) {
		this(new Date(m));
	}

	public MTime(String strTime) {
		this(strTime, true);
	}

	public MTime(java.sql.Date date) {
		this((Date) date);
	}

	public MTime(Date date) {
		this((new SimpleDateFormat("HH:mm:ss")).format(date));
	}

	public String toString() {
		return value == null ? "" : value;
	}

	public boolean after(MTime when) {
		return value.compareTo(when.toString()) > 0;
	}

	public boolean before(MTime when) {
		return value.compareTo(when.toString()) < 0;
	}

	public Object clone() {
		return new MTime(value);
	}

	public int compareTo(MTime when) {
		return value.compareTo(when.toString());
	}

	public boolean equals(Object o) {
		if ((o != null) && (o instanceof MTime)) {
			return value.equals(o.toString());
		}
		return false;
	}

	public int getHour() {
		return Integer.valueOf(value.substring(0, 2)).intValue();
	}

	public int getMinute() {
		return Integer.valueOf(value.substring(3, 5)).intValue();
	}

	public int getSecond() {
		return Integer.valueOf(value.substring(6, 8)).intValue();
	}

	public static String getValidUFTimeString(String sTime) {
		if (sTime == null)
			return null;
		if (isAllowTime(sTime))
			return sTime;
		else {
			try {
				int hour = 0;
				int minute = 0;
				int second = 0;
				int index = sTime.indexOf(":");
				if (index < 1) {
					if (sTime.trim().length() > 0)
						hour = Integer.parseInt(sTime.trim());
				} else {
					hour = Integer.parseInt(sTime.trim().substring(0, index));
					String sTemp = sTime.trim().substring(index + 1);
					if (sTemp.trim().length() > 0) {
						index = sTemp.indexOf(":");
						if (index < 1) {
							minute = Integer.parseInt(sTemp.trim());
						} else {
							minute = Integer.parseInt(sTemp.trim().substring(0, index));
							if (sTemp.trim().substring(index + 1).trim().length() > 0)
								second = Integer.parseInt(sTemp.trim().substring(index + 1));
						}
					}
				}
				if (hour < 0 || hour > 24 || minute < 0 || minute > 59 || second < 0 || second > 59)
					return null;
				String strHour = String.valueOf(hour);
				if (strHour.length() < 2)
					strHour = "0" + strHour;
				String strMinute = String.valueOf(minute);
				if (strMinute.length() < 2)
					strMinute = "0" + strMinute;
				String strSecond = String.valueOf(second);
				if (strSecond.length() < 2)
					strSecond = "0" + strSecond;
				//
				return strHour + ":" + strMinute + ":" + strSecond;
			} catch (Exception e) {
				return null;
			}
		}
	}

	public static boolean isAllowTime(String strTime) {
		if (strTime == null || strTime.trim().length() == 0)
			return true;
		if (strTime.trim().length() != 8)
			return false;
		for (int i = 0; i < 8; i++) {
			char c = strTime.trim().charAt(i);
			if (i == 2 || i == 5) {
				if (c != ':')
					return false;
			} else if (c < '0' || c > '9')
				return false;
		}
		int hour = Integer.parseInt(strTime.trim().substring(0, 2));
		int minute = Integer.parseInt(strTime.trim().substring(3, 5));
		int second = Integer.parseInt(strTime.trim().substring(6, 8));
		if (hour < 0 || hour > 24 || minute < 0 || minute > 59 || second < 0 || second > 59)
			return false;
		return true;
	}

	public MTime(long m, Object o) {
		if (m == 24 * 3600000) {
			value = "24:00:00";
			return;
		}
		long seconds = m / 1000;
		long hour = seconds / 3600;
		hour %= 24;
		long minute = seconds / 60;
		minute %= 60;
		long second = seconds % 60;
		value = "";
		if (hour < 10)
			value += "0" + hour;
		else
			value += hour;
		value += ":";
		if (minute < 10)
			value += "0" + minute;
		else
			value += minute;
		value += ":";
		if (second < 10)
			value += "0" + second;
		else
			value += second;
	}

	public MTime(String strTime, boolean isParse) {
		if (isParse)
			value = getValidUFTimeString(strTime);
		else
			value = strTime;
	}

	public long getMillis() {
		return ((getHour() * 60 + getMinute()) * 60 + getSecond()) * 1000;
	}

	public int compareTo(Object obj) {
		if (obj instanceof MTime) {
			return value.compareTo(obj.toString());
		} else {
			StringBuilder sb = new StringBuilder();
			sb.append("比较的对象不是[").append(this.getClass().getName()).append("]类型");
			throw new RuntimeException(sb.toString());
		}
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

}